var app = getApp()
Page({
    data:{
        dormList: [],
        selectedDorm: -1,
        step: 0,
        floorList: [],
        roomList: []
    },
    onLoad: function(opt){
        wx.setNavigationBarTitle({
            title: '选择宿舍',
            success: function(res) {
                // success
            }
        })
        console.log(opt)
        var dormData = opt.data
        if(!dormData){
            this.goLogin()
            return
        }
        var dormDataList = dormData.split("|")
        var dormList = []
        if(dormDataList.length > 1){
            for(var i=0; i<dormDataList.length; i++){
                var compo = dormDataList[i].split(",")
                if(compo.length > 1){
                    var key = compo[0]
                    var value = compo[1]
                    dormList.push({id: key, name:value})
                }
            }
            console.log(dormList)
            this.setData({
                dormList: dormList,
                cookie: app.globalData.ecardCookie,
                screenHeight: wx.getSystemInfoSync().windowHeight
            })
            console.log(this.data.screenHeight)
        }else{
            this.goLogin()
        }
    },
    goLogin: function(){
        wx.showModal({
            title: '提示',
            content: '登陆状态不可用，请重新登陆',
            showCancel: false,
            success: function(res){
                if(res.confirm){
                    wx.reLaunch({
                        url: '../index/index',
                        success: function(res){
                        // success
                        },
                        fail: function(res) {
                        // fail
                        },
                        complete: function(res) {
                        // complete
                        }
                    })
                }
            }
        })
    },
    dormRowTap: function(e){
        console.log(e)
        this.setData({
            selectedDorm: e.currentTarget.dataset.index
        })
        var dormID = this.data.dormList[e.currentTarget.dataset.index].id
        if(dormID){
            this.setData({
                floorList: [],
                selectedFloor: -1,
                step: 1
            })
            wx.showNavigationBarLoading()
            var _this = this
            wx.request({
              url: 'https://hcdstudio.cn/icard/wx/getRoom.php',
              header: {
                  "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8;"
              },
              data: "cookie="+this.data.cookie+"&building="+dormID,
              method: 'POST',
              success: function(res){
                  console.log(res.data)
                  if(res.data){
                      var resData = res.data.split("|")
                      var floorList = []
                      if(resData.length > 1){
                          for(var i=0; i<resData.length; i++){
                              var compo = resData[i].split(",")
                              if(compo.length > 1){
                                  var key = compo[0]
                                  var floorInfo = compo[1].split("-")
                                  var value = ''
                                  if(floorInfo.length > 1){
                                      value = floorInfo[1]
                                  }else{
                                      value = compo[1]
                                  }
                                  floorList.push({ id: key, name: value, raw: compo[1]})
                              }
                          }
                      }
                      _this.setData({
                          floorList: floorList
                      })
                  }
              },
              fail: function(res) {
                // fail
              },
              complete: function(res) {
                // complete
                wx.hideNavigationBarLoading()
              }
            })
        }
    },
    floorRowTap: function(e){
        this.setData({
            selectedFloor: e.currentTarget.dataset.index
        })
        var dormID = this.data.dormList[this.data.selectedDorm].id
        var floorID = this.data.floorList[e.currentTarget.dataset.index].id
        if(floorID && dormID){
            this.setData({
                roomList: [],
                selectedRoom: -1,
                step: 2
            })
            wx.showNavigationBarLoading()
            var _this = this
            wx.request({
              url: 'https://hcdstudio.cn/icard/wx/getRoom.php',
              header: {
                  "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8;"
              },
              data: "cookie="+this.data.cookie+"&building="+dormID+"&floor="+floorID,
              method: 'POST',
              success: function(res){
                  console.log(res.data)
                  if(res.data){
                      var resData = res.data.split("|")
                      var roomList = []
                      if(resData.length > 1){
                          for(var i=0; i<resData.length; i++){
                              var compo = resData[i].split(",")
                              if(compo.length > 1){
                                  var key = compo[0]
                                  var floorInfo = compo[1].split("-")
                                  var value = ''
                                  if(floorInfo.length > 1){
                                      value = floorInfo[1]
                                  }else{
                                      value = compo[1]
                                  }
                                  roomList.push({ id: key, name: value, raw: compo[1]})
                              }
                          }
                      }
                      _this.setData({
                          roomList: roomList
                      })
                  }
              },
              fail: function(res) {
                // fail
              },
              complete: function(res) {
                // complete
                wx.hideNavigationBarLoading()
              }
            })
        }
    },
    roomRowTap: function(e){
        this.setData({
            selectedRoom: e.currentTarget.dataset.index
        })
        var dormID = this.data.dormList[this.data.selectedDorm].id
        var floorID = this.data.floorList[this.data.selectedFloor].id
        var roomID = this.data.roomList[e.currentTarget.dataset.index].id

        var dormName = ''
        var buildName = this.data.dormList[this.data.selectedDorm].name
        if(buildName){
            dormName += buildName
        }
        var roomName = this.data.roomList[e.currentTarget.dataset.index].name
        if(roomName){
            dormName += roomName
        }

        if(dormID && floorID && roomID){
          wx.setStorageSync('selDorm', { dorm: dormID, floor: floorID, room: roomID, name: dormName, raw: { 
              build: this.data.dormList[this.data.selectedDorm].name, 
              floor: this.data.floorList[this.data.selectedFloor].raw, 
              room: this.data.roomList[e.currentTarget.dataset.index].raw
              }
            })
            wx.navigateBack({
              delta: 1, // 回退前 delta(默认为1) 页面
              success: function(res){
                // success
              },
              fail: function(res) {
                // fail
              },
              complete: function(res) {
                // complete
              }
            })
        }
    }
})